import { defineStore } from 'pinia'

// 常规写法 类似vuex
export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 100
  }),
  getters: {
    doubleCount: (state) => state.count * 2
  },
  actions: {
    increment() {
      this.count++
    },
    decrement() {
      this.count--
    },
    incrementIfOdd() {
      if (this.count % 2 === 1) {
        this.count++
      }
    },
    incrementAsync() {
      setTimeout(() => {
        this.count++
      }, 1000)
    }
  },
  // 开启数据缓存
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'counter', //设置存储的key
        storage: localStorage //表示存储在localStorage
        //  paths: ['id'],//指定要长久化的字段
      }
    ]
  }
})
